package com.zqx.memento.client;

import com.zqx.memento.originalAndMementoAndCaretaker.Chessman;
import com.zqx.memento.originalAndMementoAndCaretaker.MementoCaretaker;

/**
 * description: 客户端测试类
 * @author zqx
 * @date 2018/09/04
 */
public class Client {
    private static int index = -1;//定义一个索引来记录当前状态所在的位置
    private static MementoCaretaker mementoCaretaker = new MementoCaretaker();
    public static void main(String[] args) {
        //MementoCaretaker mementoCaretaker = new MementoCaretaker();
        Chessman chessman = new Chessman("帥", 1, 1);
        display(chessman);
        //mementoCaretaker.setMemento(chessman.save());//保存状态
        chessman.setY(4);
        display(chessman);
        //mementoCaretaker.setMemento(chessman.save());//保存状态
        chessman.setX(5);
        display(chessman);
        /*System.out.println("=================悔棋==============");
        chessman.restore(mementoCaretaker.getMemento());
        display(chessman);*/
        undo(chessman,index);
        undo(chessman,index);
        redo(chessman,index);
        redo(chessman,index);

    }

    //撤销悔棋
    private static void redo(Chessman chessman, int i) {
        System.out.println("&&&&&&&&&&&撤销悔棋&&&&&&&&&&&");
        index ++;
        chessman.restore(mementoCaretaker.getMemento(i+1));//恢复到下一个备忘录
        System.out.println("棋子"+chessman.getLabel()+"当前的位置为："
                +"第"+chessman.getX()+"行"+"第"+chessman.getY()+"列。");
    }

    //悔棋
    private static void undo(Chessman chessman, int i) {
        System.out.println("==========悔棋=================");
        index --;
        chessman.restore(mementoCaretaker.getMemento(i-1));//撤销到上一个备忘录
        System.out.println("棋子"+chessman.getLabel()+"当前的位置为："
                +"第"+chessman.getX()+"行"+"第"+chessman.getY()+"列。");
    }

    //下棋
    private static void display(Chessman chessman) {
        mementoCaretaker.setMemento(chessman.save());//保存备忘录
        index ++;
        System.out.println("棋子"+chessman.getLabel()+"当前的位置为："
                +"第"+chessman.getX()+"行"+"第"+chessman.getY()+"列。");
    }
}
